#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    int subarraysDivByK(vector<int> &nums, int k)
    {
        int sum = 0;
        int ans = 0;
        
        unordered_map<int, int> mp;
        mp[0] = 1;
        for (int &t : nums)
        {
            sum = (sum + t) % k;
            sum = (sum + k) % k;

            ans += mp[sum];
            mp[sum]++;
        }
        return ans;
    }
};